/**
 * Author : Fengyuan(Franklin) Zhang
 * Date : 2019/1/23
 * Description : Server
 */

var mongoose = require('./mongooseModel');
var BaseModel = require('./baseModel');

var Servers = function (server) {
    this.s_ip = '';
    this.s_port = 0;
    this.s_mac = '';
    this.s_type = 0;
    this.s_services = [];
    this.s_status = false;
    this.s_user = '';
    this.s_hardware = {};
    this.s_software = [];
    this.s_dynamic = {};
    this.s_datetime = {};
    this.s_runningtask = 0;
    if(server && server.s_ip){
        this.s_ip = server.s_ip;
    }
    if(server && server.s_port){
        this.s_port = server.s_port;
    }
    if(server && server.s_mac){
        this.s_mac = server.s_mac;
    }
    if(server && server.s_type){
        this.s_type = server.s_type;
    }
    if(server && server.s_services){
        this.s_services = server.s_services;
    }
    if(server && server.s_status){
        this.s_status = server.s_status;
    }
    if(server && server.s_user){
        this.s_user = server.s_user;
    }
    if(server && server.s_hardware){
        this.s_hardware = server.s_hardware;
    }
    if(server && server.s_software){
        this.s_software = server.s_software;
    }
    if(server && server.s_dynamic){
        this.s_dynamic = server.s_dynamic;
    }
    if(server && server.s_datetime){
        this.s_datetime = server.s_datetime;
    }
    if(server && server.s_runningtask){
        this.s_runningtask = server.s_runningtask;
    }
    return this;
}

Servers.__proto__ = BaseModel;
module.exports = Servers;

var serverSchame = new mongoose.Schema({
    s_ip : String,
    s_port : Number,
    s_mac : String,
    s_type : Number,
    s_services : Array,
    s_status: Boolean,
    s_user: String,
    s_hardware:mongoose.Schema.Types.Mixed,
    s_software: Array,
    s_dynamic: mongoose.Schema.Types.Mixed,
    s_datetime: Date,
    s_runningtask: Number
},{collection:'server'});
var serverModel = mongoose.model('server', serverSchame);
Servers.baseModel = serverModel;
Servers.modelName = "Server Model";

Servers.getByMac = function (mac, callback) {
    this.getByWhere({s_mac : mac}, this.returnFunction(callback, 'Error in getting servers by mac address'));
};

Servers.getAll = function (callback) {
    this.getByWhere({}, this.returnFunction(callback, 'Error in getting all servers'));
};

Servers.getByIP = function (ip, callback) {
    this.getByWhere({s_ip : ip}, this.returnFunction(callback, 'Error in getting servers by mac address'));
};

Servers.getByPID = function (pid, callback) {
    this.getByWhere({s_services : pid}, this.returnFunction(callback, 'Error in getting servers by pid'));
};

Servers.getAll = function(callback){
    this.getByWhere({}, this.returnFunction(callback, 'Error in getting all servers'));
};

Servers.getByPIDWithStatus = function(pid, status, callback) {
    this.getByWhere({s_services: pid, s_status: status}, this.returnFunction(callback, 'Erro in getting servers by model pid'));
}

Servers.getByTypeWithStatus = function(type, status, callback){
    this.getByWhere({s_type: type, s_status: status}, this.returnFunction(callback, 'Error in getting servers by type'));
};

Servers.getAvailable = function(callback){
    this.getByWhere({s_status: true}, this.returnFunction(callback, 'Error in getring all available Service'));
}

Servers.getById = function(id,callback){
    this.getByOID(id, this.returnFunction(callback, 'Error in get server by id'));
}